package com.tensquare.spit.service;

import com.tensquare.spit.dao.SpitDao;
import com.tensquare.spit.pojo.Spit;
import com.tensquare.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class SpitService {
    @Autowired
    private SpitDao spitDao;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private MongoTemplate mongoTemplate;

    public void updateThumbup(String id) {
        //方法一：交互两次mongoDB数据库
//        Spit spit = findById(id);
//        spit.setThumbup(spit.getThumbup() == null ? 1 : spit.getThumbup() + 1);
//        spitDao.save(spit);
        //方法二：只交互一次
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update = new Update();
        update.inc("thumbup", 1);
        mongoTemplate.updateFirst(query, update, "spit");
    }

    /**
     * 根据上级ID查询吐槽列表
     *
     * @param parentid
     * @param page
     * @param size
     * @return
     */
    public Page<Spit> findByParentid(String parentid, int page, int size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        return spitDao.findByParentid(parentid, pageable);
    }

    public List<Spit> findAll() {
        return spitDao.findAll();
    }

    public Spit findById(String id) {
        return spitDao.findById(id).get();
    }

    public void save(Spit spit) {
        spit.set_id(idWorker.nextId() + "");
        spit.setPublishtime(new Date());//发布日期
        spit.setVisits(0);//浏览量
        spit.setShare(0);//分享数
        spit.setThumbup(0);//点赞数
        spit.setComment(0);//回复数
        spit.setState("1");//状态
        if (null != spit.getParentid() && !"".equals(spit.getParentid())) {
            Query query = new Query().addCriteria(Criteria.where("_id").is(spit.getParentid()));
            Update update = new Update().inc("comment", 1);
            mongoTemplate.updateFirst(query, update, "spit");
        }
        spitDao.save(spit);
    }

    public void update(Spit spit) {
        spitDao.save(spit);
    }

    public void deleteById(String id) {
        spitDao.deleteById(id);
    }

    public List<Spit> searchByQuery(Spit spit) {
        Criteria criteria = new Criteria();
        if (null != spit.getContent() && !"".equals(spit.getContent())) {
            criteria.and("content").is(spit.getContent());
        }
        if (null != spit.getPublishtime() && !"".equals(spit.getPublishtime())) {
            criteria.and("publishtime").is(spit.getPublishtime());
        }
        if (null != spit.getUserid() && !"".equals(spit.getUserid())) {
            criteria.and("userid").is(spit.getUserid());
        }
        if (null != spit.getNickname() && !"".equals(spit.getNickname())) {
            criteria.and("nickname").is(spit.getNickname());
        }
        if (null != spit.getVisits()) {
            criteria.and("visits").is(spit.getVisits());
        }
        if (null != spit.getThumbup()) {
            criteria.and("thumbup").is(spit.getThumbup());
        }
        if (null != spit.getShare()) {
            criteria.and("share").is(spit.getShare());
        }
        if (null != spit.getComment()) {
            criteria.and("comment").is(spit.getComment());
        }
        if (null != spit.getState() && !"".equals(spit.getState())) {
            criteria.and("state").is(spit.getState());
        }
        if (null != spit.getParentid() && !"".equals(spit.getParentid())) {
            criteria.and("parentid").is(spit.getParentid());
        }
        Query query = new Query().addCriteria(criteria);
        return mongoTemplate.find(query, Spit.class, "spit");
    }

    public List<Spit> searchPage(Spit spit, int page, int size) {
        Criteria criteria = new Criteria();
        if (null != spit.getContent() && !"".equals(spit.getContent())) {
            criteria.and("content").is(spit.getContent());
        }
        if (null != spit.getPublishtime() && !"".equals(spit.getPublishtime())) {
            criteria.and("publishtime").is(spit.getPublishtime());
        }
        if (null != spit.getUserid() && !"".equals(spit.getUserid())) {
            criteria.and("userid").is(spit.getUserid());
        }
        if (null != spit.getNickname() && !"".equals(spit.getNickname())) {
            criteria.and("nickname").is(spit.getNickname());
        }
        if (null != spit.getVisits()) {
            criteria.and("visits").is(spit.getVisits());
        }
        if (null != spit.getThumbup()) {
            criteria.and("thumbup").is(spit.getThumbup());
        }
        if (null != spit.getShare()) {
            criteria.and("share").is(spit.getShare());
        }
        if (null != spit.getComment()) {
            criteria.and("comment").is(spit.getComment());
        }
        if (null != spit.getState() && !"".equals(spit.getState())) {
            criteria.and("state").is(spit.getState());
        }
        if (null != spit.getParentid() && !"".equals(spit.getParentid())) {
            criteria.and("parentid").is(spit.getParentid());
        }
        Query query = new Query().addCriteria(criteria);
        Pageable pageable = PageRequest.of(page - 1, size);
        return mongoTemplate.find(query.with(pageable), Spit.class, "spit");
    }
}
